//index.js
//获取应用实例
var ajaxUrl = require('../../utils/url.js');
const app = getApp()

Page({
  data: {
    showModalStatus: false,  //先设置隐藏
    imgUrls: [],
    indicatorDots: true,
    autoplay: true,
    interval: 4000,
    duration: 500,
    circular: true,
    scrollHeight: 0,
    listData: [],
    category: [{label: '中国',id:'1', color: '#fd8934'},{label: '泰国',id:'2', color: '#fa4439'},{label:'缅甸', id:'3', color:'#916c5c'},{label: '越南',id: '4', color: '#0dcdb4'}],
    current: 1,
    size: 50,
    pages: 1,
    hidden:false
  },
  onShow: function () {
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          scrollHeight: res.windowHeight - (100 * res.windowWidth / 750) //80为顶部搜索框区域高度 rpx转px 屏幕宽度/750
        });
      }
    })
  },
  onLoad: function () {
    var self = this;
    //ajax请求数据
   self.fetchHomeImg()
  },
  fetchHomeImg () {
    const self = this
    wx.request({
      method: "POST",
      url: ajaxUrl.homeImg(),
      data: {},
      success: function(res){
        if(res.data.code === 200){
          self.setData({
            imgUrls: res.data.data
          })
        }else{
          console.error(res.data.msg)
        }
        self.fetchData()
      }
    })
  },
  fetchData () {
    var self = this;
    self.setData({
      hidden: false
    });
    wx.request({
      method:"POST",
      url: ajaxUrl.goodsList(),
      data: {
        currentPage: self.data.current,
        pageSize: self.data.size
      },
      header: {
        'Content-Type': 'application/json'
      },
      success: function(res) {
       
        if(res.data.code === 200){
          self.setData({
            'listData' : self.data.listData.concat(res.data.data),
          })
        }else{
          console.log(res.data.msg)
        }
        setTimeout(function () {
          self.setData({
            hidden: true
          });
        }, 300);
      }
    })
  },
  toCategoryPage: function (e) {
    var bid = e.currentTarget.dataset.bid;
    wx.navigateTo({
      url: '../category/category?id=' + bid
    });
  },
  lower:function(e){
    var self = this;
    self.setData({
      current: self.data.current + 1
    });
    self.fetchData();
  },
  toDetailPage: function (e) {
    var bid = e.currentTarget.dataset.bid;
    wx.navigateTo({
      url: '../detail/detail?id=' + bid
    });
  }

})
